package com.sander.guardmanagement.ui.view;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.etsy.android.grid.StaggeredGridView;
import com.sander.guardmanagement.R;
import com.sander.guardmanagement.presenter.OnItemOnclickUniteListener;
import com.sander.guardmanagement.response.ResponseHomeGetSubCommunityData;
import com.sander.guardmanagement.ui.model.BoxItem;
import com.sander.guardmanagement.ui.model.DistrictBoxModel;
import com.sander.guardmanagement.until.DensityUtil;

import java.util.List;

/**
 * Created by sander on 2017/4/21.
 */


public class DistrictBoxAdapter extends BaseAdapter  {
    private Context context;

    private LayoutInflater inflater;

    public List<ResponseHomeGetSubCommunityData> boxList;

    public void setClickListener(OnItemOnclickUniteListener clickListener) {
        this.clickListener = clickListener;
    }

    private OnItemOnclickUniteListener clickListener;

    public DistrictBoxAdapter(Context context, List<ResponseHomeGetSubCommunityData> list){
        this.context = context;
        inflater = LayoutInflater.from(this.context);
        this.boxList = list;
    }
    @Override
    public int getCount() {
        return boxList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BoxItem item = null;
        ResponseHomeGetSubCommunityData model = boxList.get(position);

        if (convertView == null){
            convertView = this.inflater.inflate(R.layout.districtboxitem,parent,false);
            item = new BoxItem();
            item.name_textView = (TextView) convertView.findViewById(R.id.roottext);
            item.unit_listView = (StaggeredGridView)convertView.findViewById(R.id.unitlistview);
            DistrictUnitAdapter unitAdapter = new DistrictUnitAdapter(context,model.getBuilding_list());
            item.unit_listView.setAdapter(unitAdapter);
            item.unit_listView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return false;
                }
            });
            final int section = position;
            item.unit_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    clickListener.onClickItem(position,section);
                }
            });


            convertView.setTag(item);
        }else{
            item = (BoxItem) convertView.getTag();
        }
        int row = model.getBuilding_list().size() / 2;
        int section = model.getBuilding_list().size() % 2;
        if (section > 0 ){
            row += 1;
        }
        Log.d("sander","Params height = " + row);
        ViewGroup.LayoutParams params = convertView.getLayoutParams();
        params.height =  (row * (DensityUtil.dip2px(context,50))) + (row - 1) * DensityUtil.dip2px(context,5) + DensityUtil.dip2px(context,10);
        convertView.setLayoutParams(params);
        DistrictUnitAdapter adapter = (DistrictUnitAdapter) item.unit_listView.getAdapter();
        adapter.unitList = model.getBuilding_list();
        adapter.notifyDataSetChanged();
        item.name_textView.setText(model.getSub_community_name());
        return convertView;
    }
}

